package sample;

import org.w3c.dom.*;

import javax.xml.parsers.*;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class DOM4JTest {
    public static String attrValue(String src,String attrName) throws Exception {
        //得到解析器
        //System.out.println("aaaaa");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        //通过解析器就可以得到代表整个内存中XML的Document对象
        File file = new File(src);
        String url = file.toURI().toURL().toString();
        if(BeautyMain.themeURL==null||BeautyMain.themeURL==""){
            return null;
        }

        System.out.println(url);
        Document document = builder.parse(url);
       String color =  test1(document,attrName);
       color=color.split("#")[1];
       String A="";
       String R ="";
       String G ="";
       String B ="";
       if(color.length()==8){
            A=color.substring(0,2);
            Integer a = Integer.parseInt(A,16);
            R=color.substring(2,4);
           Integer r = Integer.parseInt(R,16);
            G=color.substring(4,6);
           Integer g = Integer.parseInt(G,16);
            B=color.substring(6);
           Integer b = Integer.parseInt(B,16);
            color = r+","+g+","+b+","+a;
       }else{
           R=color.substring(0,2);
           Integer r = Integer.parseInt(R,16);
           G=color.substring(2,4);
           Integer g = Integer.parseInt(G,16);
           B=color.substring(4);
           Integer b = Integer.parseInt(B,16);
           color = r+","+g+","+b;
       }


        return color;
    }


    private static String  test1(Document document,String attrName){
        NodeList nodeList = document.getElementsByTagName("vivo_theme_values");
        String attrValue = null;
        Element element= (Element) nodeList.item(0);
        NodeList nodeList2=  element.getChildNodes();

        for(int i=0;i<nodeList2.getLength();i++){
            if(nodeList2.item(i).getNodeName().equals("#text")){

            }else{
                String name = nodeList2.item(i).getAttributes().getNamedItem("name").getTextContent();
                if(name.equals(attrName)){
                 attrValue = nodeList2.item(i).getTextContent();
                return attrValue;
                }
            }
        }

        return attrValue;
    }

//    public static void main(String [] args ) throws Exception{
//        String src="E:\\第二阶段素材\\初恋的味道\\com.vivo.dream.music\\theme_values.xml";
//        String attrName = "dream_music_song_name";
//        //attrValue(src,attrName);
//        System.out.println(attrValue(src,attrName));
//    }


}


